﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppData;
using System.Data;
using System.Data.SqlClient;
using AppFormat;

namespace BFOAuction.DAL.Auction
{
    public class DAL_Auction
    {
        public AppFormat.MyError LoadInfo(ref DTO.Auction.Auction objAuction, string strAuctionID)
        {
            AppFormat.MyError Error = new AppFormat.MyError();
            ConnectData objCon = new ConnectData();
            try
            {
                objCon.StrConnect = "data source=LANGTUBUON-PC;initial catalog=BFOAuction;user id=sa;password=saa;";
                objCon.CreateConnect();
                SqlParameter[] p = new SqlParameter[1];
                p[0] = new SqlParameter("@AuctionID", strAuctionID);

                DataTable dt = objCon.getTableProcedure("AU_Auction_SEL", p);
                if (dt != null && dt.Rows.Count > 0)
                {
                    objAuction = new DTO.Auction.Auction();
                    objAuction.AuctionID = FormatData.IsNull(dt.Rows[0]["AuctionID"].ToString(), "").ToString();
                    objAuction.DateBegin = Convert.ToDateTime(FormatData.IsNull(dt.Rows[0]["DateBegin"], DateTime.Now));
                    objAuction.DateEnd = Convert.ToDateTime(FormatData.IsNull(dt.Rows[0]["DateEnd"], DateTime.Now));
                    objAuction.PriceBegin = Convert.ToDecimal(FormatData.IsNull(dt.Rows[0]["PriceBegin"], 0));
                    objAuction.PriceIncreases = Convert.ToDecimal(FormatData.IsNull(dt.Rows[0]["PriceIncreases"], 0));
                    objAuction.Cost = Convert.ToDecimal(FormatData.IsNull(dt.Rows[0]["m_Cost"], 0));
                    objAuction.Note = dt.Rows[0]["Note"].ToString();
                    objAuction.Total = Convert.ToDecimal(FormatData.IsNull(dt.Rows[0]["Total"], 0));
                    objAuction.UserCreate = dt.Rows[0]["UserCreate"].ToString();
                }
            }
            catch (Exception ex)
            {
                objCon.ClosedConnect();
                Error.IsError = true;
                Error.TypeError = AppFormat.MyError.enTypeError.Select;
                Error.MessageErrorDetail = objCon.ReturnMyError.MessageErrorDetail;
                Error.MessageError = "Lỗi lấy thông tin cấu hình ( " + ex.Message + " )";
            }
            finally
            {
                objCon.ClosedConnect();
            }
            return Error;
        }

        public AppFormat.MyError Insert(DTO.Auction.Auction objAuction)
        {
            AppFormat.MyError Error = new AppFormat.MyError();
            ConnectData objCon = new ConnectData();
            try
            {
                objCon.StrConnect = "data source=LANGTUBUON-PC;initial catalog=BFOAuction;user id=sa;password=saa;";
                objCon.CreateConnect();
                SqlParameter[] p = new SqlParameter[9];
                p[0] = new SqlParameter("@" + DTO.Auction.Auction.colAuctionID, objAuction.AuctionID);
                p[1] = new SqlParameter("@" + DTO.Auction.Auction.colDateBegin, objAuction.DateBegin);
                p[2] = new SqlParameter("@" + DTO.Auction.Auction.colDateEnd, objAuction.DateEnd);
                p[3] = new SqlParameter("@" + DTO.Auction.Auction.colPriceBegin, objAuction.PriceBegin);
                p[4] = new SqlParameter("@" + DTO.Auction.Auction.colPriceIncreases, objAuction.PriceIncreases);
                p[5] = new SqlParameter("@" + DTO.Auction.Auction.colCost, objAuction.Cost);
                p[6] = new SqlParameter("@" + DTO.Auction.Auction.colNote, objAuction.Note);
                p[7] = new SqlParameter("@" + DTO.Auction.Auction.colTotal, objAuction.Total);
                p[8] = new SqlParameter("@" + DTO.Auction.Auction.colUserCreate, objAuction.UserCreate);

                objCon.execProc("AU_Auction_ADD", p);

            }
            catch (Exception ex)
            {
                objCon.ClosedConnect();
                Error.IsError = true;
                Error.TypeError = AppFormat.MyError.enTypeError.Select;
                Error.MessageErrorDetail = objCon.ReturnMyError.MessageErrorDetail;
                Error.MessageError = "Lỗi thêm thông tin đấu giá ( " + ex.Message + " )";
            }
            finally
            {
                objCon.ClosedConnect();
            }
            return Error;
        }

        public AppFormat.MyError Update(DTO.Auction.Auction objAuction)
        {
            AppFormat.MyError Error = new AppFormat.MyError();
            ConnectData objCon = new ConnectData();
            try
            {
                objCon.StrConnect = "data source=LANGTUBUON-PC;initial catalog=BFOAuction;user id=sa;password=saa;";
                objCon.CreateConnect();
                SqlParameter[] p = new SqlParameter[9];
                p[0] = new SqlParameter("@" + DTO.Auction.Auction.colAuctionID, objAuction.AuctionID);
                p[1] = new SqlParameter("@" + DTO.Auction.Auction.colDateBegin, objAuction.DateBegin);
                p[2] = new SqlParameter("@" + DTO.Auction.Auction.colDateEnd, objAuction.DateEnd);
                p[3] = new SqlParameter("@" + DTO.Auction.Auction.colPriceBegin, objAuction.PriceBegin);
                p[4] = new SqlParameter("@" + DTO.Auction.Auction.colPriceIncreases, objAuction.PriceIncreases);
                p[5] = new SqlParameter("@" + DTO.Auction.Auction.colCost, objAuction.Cost);
                p[6] = new SqlParameter("@" + DTO.Auction.Auction.colNote, objAuction.Note);
                p[7] = new SqlParameter("@" + DTO.Auction.Auction.colTotal, objAuction.Total);
                p[8] = new SqlParameter("@" + DTO.Auction.Auction.colUserCreate, objAuction.UserCreate);

                objCon.execProc("AU_Auction_UPD", p);

            }
            catch (Exception ex)
            {
                objCon.ClosedConnect();
                Error.IsError = true;
                Error.TypeError = AppFormat.MyError.enTypeError.Select;
                Error.MessageErrorDetail = objCon.ReturnMyError.MessageErrorDetail;
                Error.MessageError = "Lỗi cập nhật thông tin đấu giá ( " + ex.Message + " )";
            }
            finally
            {
                objCon.ClosedConnect();
            }
            return Error;
        }

        public AppFormat.MyError Delete(string strAuctionID)
        {
            AppFormat.MyError Error = new AppFormat.MyError();
            ConnectData objCon = new ConnectData();
            try
            {
                objCon.StrConnect = "data source=LANGTUBUON-PC;initial catalog=BFOAuction;user id=sa;password=saa;";
                objCon.CreateConnect();
                SqlParameter[] p = new SqlParameter[1];
                p[0] = new SqlParameter("@AuctionID", strAuctionID);

                objCon.execProc("AU_Auction_DEL", p);

            }
            catch (Exception ex)
            {
                objCon.ClosedConnect();
                Error.IsError = true;
                Error.TypeError = AppFormat.MyError.enTypeError.Select;
                Error.MessageErrorDetail = objCon.ReturnMyError.MessageErrorDetail;
                Error.MessageError = "Lỗi xóa thông tin đấu giá ( " + ex.Message + " )";
            }
            finally
            {
                objCon.ClosedConnect();
            }
            return Error;
        }

        public AppFormat.MyError Select(ref DataTable dtResul, string strAuctionID)
        {
            AppFormat.MyError Error = new AppFormat.MyError();

            ConnectData objCon = new ConnectData();
            try
            {
                objCon.StrConnect = "data source=LANGTUBUON-PC;initial catalog=BFOAuction;user id=sa;password=saa;";
                objCon.CreateConnect();
                SqlParameter[] p = new SqlParameter[1];
                p[0] = new SqlParameter("@AuctionID", strAuctionID);

                dtResul = objCon.getTableProcedure("AU_Auction_SEL", p);

            }
            catch (Exception ex)
            {
                objCon.ClosedConnect();
                Error.IsError = true;
                Error.TypeError = AppFormat.MyError.enTypeError.Select;
                Error.MessageErrorDetail = objCon.ReturnMyError.MessageErrorDetail;
                Error.MessageError = "Lỗi nạp thông tin đấu giá ( " + ex.Message + " )";
            }
            finally
            {
                objCon.ClosedConnect();
            }
            return Error;
        }

        public AppFormat.MyError SelectByUser(ref DataTable dtResul, string strCreateUser)
        {
            AppFormat.MyError Error = new AppFormat.MyError();
            ConnectData objCon = new ConnectData();
            try
            {
                objCon.StrConnect = "data source=LANGTUBUON-PC;initial catalog=BFOAuction;user id=sa;password=saa;";
                objCon.CreateConnect();
                SqlParameter[] p = new SqlParameter[1];
                p[0] = new SqlParameter("@CreateUser", strCreateUser);
                dtResul = objCon.getTableProcedure("AU_Auction_SELBYUSER", p);

            }
            catch (Exception ex)
            {
                objCon.ClosedConnect();
                Error.IsError = true;
                Error.TypeError = AppFormat.MyError.enTypeError.Select;
                Error.MessageErrorDetail = objCon.ReturnMyError.MessageErrorDetail;
                Error.MessageError = "Lỗi tìm kiếm thông tin danh sách đấu giá ( " + ex.Message + " )";
            }
            finally
            {
                objCon.ClosedConnect();
            }
            return Error;
        }
    }
}
